# @Time : 2020/9/1 17:40
# @Author : Fioman 
# @Phone : 13149920693
import cv2 as cv
import numpy as np
from skimage.filters import threshold_local
from skimage import measure

plate = cv.imread("pic/30.png")
V = cv.split(cv.cvtColor(plate, cv.COLOR_BGR2HSV))[2]

thresh = cv.adaptiveThreshold(V, 255, cv.ADAPTIVE_THRESH_MEAN_C, cv.THRESH_BINARY_INV, 17, 5)

cv.imshow("Plate", plate)
cv.imshow("Thres", thresh)
cv.waitKey(0)
labels = measure.label(thresh, neighbors=8, background=0)
mask = np.zeros(thresh.shape, dtype=np.uint8)
print("[INFO] found {} blobs".format(len(np.unique(labels))))

for index, label in enumerate(np.unique(labels)):
    if label == 0:
        print("[INFO] label: 0 (background)")
        continue

    labelMask = np.zeros(thresh.shape, dtype=np.uint8)
    labelMask[labels == label] = 255
    cv.waitKey(0)
    numPixels = cv.countNonZero(labelMask)
    print("[INFO] label: {} (foreground),numberPixels: {}".format(index, numPixels))

    if numPixels > 700 and numPixels < 2000:
        mask = cv.add(mask, labelMask)

    cv.imshow("Label", labelMask)
    cv.waitKey(0)

cv.imshow("Large Bolobs", mask)
cv.waitKey(0)
